package com.smart.upms.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.core.starter.base.BaseController;
import com.smart.core.starter.model.R;
import com.smart.upms.sys.api.UserApi;
import com.smart.upms.sys.param.SysUserParam;
import com.smart.upms.sys.param.SysUserQueryParam;
import com.smart.upms.sys.result.SysUserAndRoleResult;
import com.smart.upms.sys.result.SysUserResult;
import com.smart.upms.sys.result.UserInfoResult;
import com.smart.upms.sys.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 系统用户 前端控制器
 * </p>
 *
 * @author guxiaobai
 * @since 2018-12-05
 */
@Api(description = "用户管理接口")
@RestController
@RequestMapping("/sys/sys-user")
@AllArgsConstructor
public class SysUserController extends BaseController implements UserApi {
    private final ISysUserService sysUserService;


    /**
     * 添加用户
     *
     * @param param 用户信息
     * @return success/false
     */
    @ApiOperation("添加用户")
    @PostMapping
    public R<Boolean> save(@Validated @RequestBody SysUserParam param) {
        return R.success(sysUserService.save(param));
    }


    /**
     * 更新用户
     *
     * @param param 用户信息
     * @return R
     */
    @ApiOperation("更新用户")
    @PutMapping
    public R<Boolean> update(@Validated @RequestBody SysUserParam param) {
        return R.success(sysUserService.update(param));

    }


    /**
     * 删除用户信息
     *
     * @param id ID
     * @return R
     */
    @ApiOperation("删除用户")
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable Long id) {
        SysUserResult result = sysUserService.get(id);
        if (result != null) {
            return R.success(sysUserService.delete(result));
        } else {
            return R.success(Boolean.FALSE);
        }
    }


    /**
     * 通过ID查询(含角色)
     *
     * @param id ID
     * @return 用户信息
     */
    @ApiOperation("通过ID查询(含角色)")
    @GetMapping("/{id}")
    public R<SysUserAndRoleResult> get(@PathVariable Long id) {
        return R.success(sysUserService.getUserAndRole(id));
    }



    /**
     * 获取当前用户信息（角色、权限）
     *
     * @param username 用户名
     * @return 用户
     */
    @ApiOperation("获取当前用户信息（角色、权限）")
    @GetMapping("/info/{username}")
    @Override
    public R<UserInfoResult> info(@PathVariable String username) {
        return R.success(sysUserService.infoByUsername(username));
    }



    /**
     * 分页查询
     *
     * @param param 查询参数
     * @return 用户集合
     */
    @ApiOperation(value = "分页查询")
    @GetMapping("/page")
    public R<Page<SysUserAndRoleResult>> page(SysUserQueryParam param) {
        Page<SysUserAndRoleResult> page = getPage(param);
        return R.success(sysUserService.listPage(page, param));
    }

}
